import * as THREE from '../jsm/three.module.js';
import { OrbitControls } from '../jsm/controls/OrbitControls.js';

import { TWEEN } from '../jsm/libs/tween.module.min.js';

import { initOtherRender, setWinAnimate } from './setWindow';

import { initWater } from './water';
import { initLand } from '../land';
import { initShip, getShipCenter } from '../ship';

let container;

let scene, camera, renderer;
let controls;

let water, land, ship; // 海 地 船

let animationFrame;

function init(mount) {

    container = mount;

    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(70, container.clientWidth / container.clientHeight, 0.5, 8000);
    renderer = new THREE.WebGLRenderer({ antialias: true });

    scene.background = new THREE.Color(0x74B8FC);
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(container.clientWidth, container.clientHeight);
    container.appendChild(renderer.domElement);

    // console.log(renderer);
    camera.position.set(0, 50, 120);

    controls = new OrbitControls(camera, renderer.domElement);
    controls.update();
    controls.autoRotate = true;
    // controls.minDistance = 100;
    controls.maxDistance = 2000;

    // camera.position.set(10.798154319596787, 19.485733026336153, -69.16875482158129);
    // controls.target.set(10.798173805319042, - 2.8500433843922066e-17, -69.16875480109397);
    // controls.update();
    // let axesHelper = new THREE.AxesHelper(1000);
    // scene.add(axesHelper);
    // console.log(axesHelper);


    let dirL = new THREE.DirectionalLight(0xffffff, 3);
    dirL.position.set(1, 1, 1);
    scene.add(dirL);

    let ambL = new THREE.AmbientLight(0xffffff, 1);
    scene.add(ambL);

    // 加载水
    water = initWater();
    scene.add(water);

    // 加载地
    land = initLand();
    scene.add(land);

    // 请求并添加船
    ship = initShip();
    scene.add(ship);

    window.out_scene = scene;
    window.out_controls = controls;

}

// 页面缩放的监听事件
function onWindowResize() {
    camera.aspect = container.clientWidth / container.clientHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(container.clientWidth, container.clientHeight);
}

// 初始化场景
function initScene(mount) {
    if (!renderer) {
        init(mount);
        initOtherRender(); // 初始化两个小窗口的

        // setTimeout(() => {
        //     fangxiashuangba();
        // }, 4000);

    } else {
        container = mount;
        container.appendChild(renderer.domElement);
        onWindowResize();
    }
    enableScene(); // 启动场景
}

// 启用场景
function enableScene() {
    window.addEventListener('resize', onWindowResize, false);
    animate();
}

// 停止场景
function stopScene() {
    if (animationFrame) cancelAnimationFrame(animationFrame);
    if (renderer) {
        window.removeEventListener('resize', onWindowResize, false);
    }
    container = null; // 容器赋空
}


// 更新视角
function updatePerspective(vecter) {
    controls.target.copy(vecter);
    camera.position.copy(controls.target.clone().add(camera.position));
    controls.update();
}

// ================================================= 切换观察视角事件区域 【开始】 =================================================
let view_way_state = 2;

// 切换相机观察船的视角
function cameraTracking() {
    let v_c_t = camera.position.clone().sub(controls.target); // 相机到控制器的距离
    controls.target.copy(getShipCenter(true, 0)); // 控制器的位置赋值 船对象的中心点位置
    camera.position.copy(controls.target.clone().add(v_c_t)); // 再重新计算相机的位置
    controls.update();
}

// 切换追踪效果
function switchTracking(val) {
    switch (val) {
        case 2:
            // controls.enable = true; // 小马哥原本是让controls.enable的
            controls.enablePan = true;
            controls.autoRotate = false;
            break;
        case 1:
            controls.enablePan = false;
            controls.autoRotate = false;
            break;
        case 3:
            controls.enablePan = false;
            controls.autoRotate = true;
            break;
        default:
            break;
    };
    view_way_state = val;
}
// ================================================= 切换观察视角事件区域 【结束】 =================================================


// ================================================= 切换舱内外视角事件区域 【开始】 =================================================
let inshipCabin = false;

// 船的视觉效果
function shipVisual(type) {
    controls.target.copy(getShipCenter(true, 0)); // 控制器的位置赋值 船对象的中心点位置
    if (type === 1) { // 舱内
        // 相机位置和控制器位置不断变化，追随船移动，且海水在渲染时候屏蔽，避免舱内显示出水
        let v = getShipCenter();
        v.x++;
        v.y /= 2;
        v.z++;
        camera.position.copy(v);
        inshipCabin = true;
    } else if (type === 2) { // 舱外
        controls.target.copy(getShipCenter(true, 0));
        camera.position.copy(controls.target.clone().add(new THREE.Vector3(0, 50, 120)));
        inshipCabin = false;
    }
    controls.update();
}
// ================================================= 切换舱内外视角事件区域 【结束】 =================================================

function animate() {
    animationFrame = requestAnimationFrame(animate);
    water.material.uniforms['time'].value += 1.0 / 60.0; // 海水移动
    TWEEN.update();

    if (view_way_state !== 2) cameraTracking();

    // 舱内视角隐藏水
    if (inshipCabin) water.visible = false;
    renderer.render(scene, camera);
    if (inshipCabin) water.visible = true;

    setWinAnimate();

}

export {
    scene,
    initScene,
    stopScene,
    switchTracking,
    shipVisual,
    updatePerspective,
};